package com.ray.base.utils;

import android.content.Context;
import android.util.Log;

import java.util.ArrayList;
import java.util.HashMap;

/**
 * @description:
 * @author: Lucas
 * @time: 2020/3/13 18:23
 */
public class SQLFunction {

    static DBHelper helper;


    public static void initTable(Context context) {
        helper = new DBHelper(context);
        helper.getReadableDatabase();
    }

    /**
     * 【插入数据】
     **/
    public static void insert(Context context, Object[] data, String tableName) {

        Log.i("TAG:", "插入数据到数据库表：" + tableName + "中:" + data.toString());

        DBManager sqlManager = new DBManager(context);
        helper = new DBHelper(context);
        helper.getWritableDatabase();
        String sql;
        if (tableName.equals("record")) {
            sql = "insert into record (title,category,duration,detail,url,id,description) values ( ? , ? , ? , ? , ? , ? , ?)";
        } else if (tableName.equals("favorite")) {
            sql = "insert into favorite (title,category,duration,detail,url,id,description) values ( ? , ? , ? , ? , ? , ? , ?)";
        } else
            sql = "insert into download (title,category,duration,detail,url,id,description) values ( ? , ? , ? , ? , ? , ? , ?)";
        Object[] bindArgs = data;
        sqlManager.updateSQLite(sql, bindArgs);
    }


    /**
     * 【模糊查询】
     **/
    public static ArrayList<HashMap<String, String>> query(Context context, String where1, String where2, String name) {
        DBManager sqlManager = new DBManager(context);
        ArrayList<HashMap<String, String>> list = new ArrayList<>();
        String sql;
        if (name.equals("record")) {
            sql = "select * from record where title like ? and url like ? ";
        } else if (name.equals("favorite")) {
            sql = "select * from favorite where title like ? and url like ?";
        } else {
            sql = "select * from download where title like ?and url like ? ";
        }
        if (where1 == null) {
            list = sqlManager.querySQLite(sql, new String[]{"%", "%"});
        } else {
            where1 = "%" + where1 + "%";
            where2 = "%" + where2 + "%";
            list = sqlManager.querySQLite(sql, new String[]{where1, where2});
        }

        Log.i("TAG:", "查询完毕，返回数据：" + list.size());

        return list;
    }

    /**
     * 【删除数据】
     **/
    public static void delete(Context context, Object[] data, String tableName) {
        DBManager sqlmanager = new DBManager(context);
        String sql;
        if (tableName.equals("record")) {
            sql = "delete from record where id =  ? ";
        } else if (tableName.equals("favorite")) {
            sql = "delete from favorite where id =  ? ";
        } else
            sql = "delete from download where id =  ? ";
        sqlmanager.updateSQLite(sql, data);
    }

//    /**
//     * 【更新数据】
//     **/
//    public static void update(Context context, Object[] data) {
//        helper = new DBHelper(context);
//        helper.getReadableDatabase();
//        DBManager sqlManager = new DBManager(context);
//        String sql = "update person set name=? , info=? where _id=?";
//        sqlManager.updateSQLite(sql, data);
//    }

    public static void deleteTable(Context context, String tableName) {
        DBManager sqlmanager = new DBManager(context);
        sqlmanager.delete(tableName);
    }
}